Ext
		.define(
				"core.room.controller.RoomController",
				{
					extend : "Ext.app.Controller",
					mixins : {
						suppleUtil : "core.util.SuppleUtil",
						messageUtil : "core.util.MessageUtil",
						formUtil : "core.util.FormUtil"

					},
					init : function() {
						var self = this;
						this
								.control({

									"panel[xtype=roomgrid]" : {
										select : this.checkEdit,
										deselect : this.checkEdit
									},
									"panel[xtype=userform] button[ref=return]" : {
										click : function(btn) {
											btn.ownerCt.ownerCt.ownerCt.close();
											return false;
										}
									},
									"panel[xtype=roomform] button[ref=saveRoom]" : {
										click : function(btn) {
											var resDel = self
													.ajax({
														url : "basicinfo/room!validateRight.action"
													});
											if (false == resDel.success) {

												Ext.Msg.alert('友情提示',
														resDel.obj);
												return false;

											}
											var roomform = btn
													.up("panel[xtype=roomform]");
											var formObj = roomform.getForm();
											var params = self
													.getFormValue(formObj);
											if (formObj.isValid()) {

												var resObj2 = self
														.ajax({
															url : "basicinfo/room!updateRoom.action",
															params : params
														});
												if (resObj2.success) {
													// self.msgbox(resObj2.obj);
													Ext.ComponentQuery
															.query("panel[xtype=roomgrid] component[xtype=pagingtoolbar]")[0]
															.doRefresh();
													btn.ownerCt.ownerCt.ownerCt
															.close();
													return false;
												} else {
													Ext.Msg.alert("友情提示",
															resObj2.obj);
													return false;
												}

											} else {
												Ext.Msg.alert('友情提示',
														"请检查房间的信息是否正确的数据");
												return false;
											}

											return false;
										}
									},

									/**
									 * 单击进入form，修改信息
									 */
									"roomgrid" : {
										itemdblclick : function(_grid, record,
												item, index, e, eOpts) {

											var window = Ext.create(
													'Ext.window.Window', {
														title : '房间信息',
														height : 380,
														width : 600,
														layout : 'fit',
														modal : true,
														items : {
															xtype : 'roomform',
															id : 'roomrform'
														}
													});
											var form = window
													.down("panel[xtype=roomform]");
											form.loadRecord(record);
											// console.log(record);
											window.show();
											return false;
										}
									},

									"roomgrid button[ref=exportToExcel]" : {
										click : function(btn) {

											window.location.href = "basicinfo/room!exportToExcel.action";

											return false;

										}
									},
									"roomgrid button[ref=searchRoom]" : {

										click : function(btn) {
											var tbar = btn.ownerCt;
											var housebh2 = tbar.down(
													"combobox[name=housebh2]")
													.getValue();
											var roomname2 = tbar
													.down(
															"textfield[name=roomname2]")
													.getValue();
											var controllerid2 = tbar
													.down(
															"textfield[name=controllerid2]")
													.getValue();
											var ipaddress = tbar
													.down(
															"textfield[name=ipaddress]")
													.getValue();
											var grid = tbar.ownerCt;
											// console.log(grid)
											// alert(housebh2)
											// alert(housebh2.length);
											var _store = grid.getStore();
											_store.clearFilter(false);
											if (housebh2 != null
													&& housebh2 != "") {
												_store.filter("housebh",
														housebh2);
											}

											/*
											 * if (roomname2 != null &&
											 * roomname2 != "") {
											 * _store.filter("roomname",
											 * roomname2); }
											 */
											if (controllerid2 != null
													&& controllerid2 != "") {
												_store.filter("controllerid",
														controllerid2);
											}

											var proxy = _store.getProxy();
											proxy.extraParams = {
												controllerid : controllerid2,
												roomname : roomname2,
												ipaddress : ipaddress
											};
											_store.loadPage(1);
											return false;

										}
									},

									"roomgrid button[ref=deleteRoom]" : {

										click : function(btn) {
											var resDel = self
													.ajax({
														url : "basicinfo/room!validateRight.action"
													});
											if (false == resDel.success) {

												Ext.Msg.alert('友情提示',
														resDel.obj);
												return false;

											}
											var grid = btn
													.up("panel[xtype=roomgrid]");
											var records = grid
													.getSelectionModel()
													.getSelection();
											if (records.length > 0) {
												var state = records[0]
														.get('roomstate');
												if ("未用" != state) {
													Ext.Msg.alert('友情提示',
															'房间为在用状态，无法进行删除');
													return false;
												}
												// 封装ids数组
												Ext.Msg
														.confirm(
																"删除房间确认",
																"<center><h3>删除后，所有人员都无法打开此门?</h3><h3>确定要删除吗?</h3></center>",
																function(result) {
																	if (result == "yes") {
																		var ids = new Array();
																		Ext
																				.each(
																						records,
																						function(
																								rec) {
																							var pkValue = rec
																									.get('roombh');
																							ids
																									.push(pkValue);
																						});
																		// 发送ajax请求
																		try {
																			var resObj = self
																					.ajax({
																						url : "basicinfo/room!deleteRoom.action",
																						params : {
																							roombhs : ids
																									.join(",")

																						}
																					});
																			if (resObj.success) {
																				grid
																						.getStore()
																						.load();
																				self
																						.msgbox(resObj.obj);
																				return false;
																			} else {
																				Ext.Msg
																						.alert(
																								'友情提示',
																								resObj.obj);
																				return false;
																			}
																		} catch (e) {
																			Ext.Msg
																					.alert(
																							'友情提示',
																							"与服务器通讯出现异常");
																		}
																	}

																});
											}

											return false;
										}
									},

									"roomgrid button[ref=ayncallroom]" : {

										click : function(btn) {
											var grid = btn.ownerCt.ownerCt;
											var store = grid.getStore();
											Ext.Msg
													.confirm(
															"同步消费系统所有房间确认",
															"<center><h3>确定要同步消费系统中的所有人员吗？<h3></center>",
															function(result) {
																if (result == "yes") {
																	// 发送ajax请求
																	var wait = Ext.Msg
																			.wait(
																					"正在同步消费系统房间信息...",
																					"请等待...");
																	var resObj = self
																			.ajax({
																				url : "basicinfo/room!syncAllRoom.action"

																			});
																	if (resObj.success) {
																		self
																				.msgbox(resObj.obj);
																		store
																				.loadPage(1);
																		wait
																				.close();
																	} else {
																		wait
																				.close();
																		Ext.Msg
																				.alert(
																						"友情提示",
																						resObj.obj);
																	}
																}
																return false;
															});
										}
									},

									"roomgangweigrid button[ref=addToGangWei]" : {
										click : function(btn) {
											var grid = btn.ownerCt.ownerCt;
											var records = grid
													.getSelectionModel()
													.getSelection();
											if (records.length == 0) {
												Ext.Msg.alert("友情提示",
														"请选择一个房间");
												return false;
											}
											var roombh = grid
													.down(
															"textfield[name=srcroombh]")
													.getValue();
											if (roombh == null || roombh == "") {
												Ext.Msg.alert("友情提示",
														"房间编号为空");
												return false;
											}
											var type = grid.down(
													"textfield[name=type]")
													.getValue();
											if (type != null && type == "del") {
												Ext.Msg
												.confirm(
														"将房间从岗位上删除",
														"<h3>确定要将"
																+ "<font color=red><b>"
																+ roombh
																+ "</b></font>房间从所选的岗位上删除并自动同步吗？</h3>",
														function(result) {
															if (result == "yes") {

																var myMask = new Ext.LoadMask(
																		Ext.ComponentQuery
																				.query("panel[xtype=centerview]")[0],
																		{
																			msg : "正在发送将房间从岗位上删除并自动同步，请您稍等..."
																		});

																if (null != myMask)
																	myMask
																			.show();
																try {
																	var ids = new Array();
																	Ext
																			.each(
																					records,
																					function(
																							rec) {
																						var pkValue = rec
																								.get('id');
																						ids
																								.push(pkValue);
																					});

																	var resObj = self
																			.ajax({
																				url : "basicinfo/room!delRoomFromGangwei.action",
																				params : {
																					gangweiids : ids
																							.join(","),
																					srcroombh : roombh
																				}
																			});
																	if (resObj.success) {
																		if (null != myMask)
																			myMask
																					.hide();
																		self
																				.msgbox(resObj.obj);
																		return false;
																	} else {
																		if (null != myMask)
																			myMask
																					.hide();
																		Ext.Msg
																				.alert(
																						"友情提示",
																						resObj.obj);
																		return false;
																	}
																} catch (e) {
																	if (null != myMask)
																		myMask
																				.hide();
																	Ext.Msg
																			.alert(
																					"友情提示",
																					"与服务器通讯出现异常");
																}

																return false;
															}
														});
												return false;
											}
											Ext.Msg
													.confirm(
															"将房间添加到岗位确认",
															"<h3>确定要将"
																	+ "<font color=red><b>"
																	+ roombh
																	+ "</b></font>房间添加到所选的岗位上，并自动下载吗？</h3>",
															function(result) {
																if (result == "yes") {

																	var myMask = new Ext.LoadMask(
																			Ext.ComponentQuery
																					.query("panel[xtype=centerview]")[0],
																			{
																				msg : "正在发送将房间添加到岗位上的命令，请您稍等..."
																			});

																	if (null != myMask)
																		myMask
																				.show();
																	try {
																		var ids = new Array();
																		Ext
																				.each(
																						records,
																						function(
																								rec) {
																							var pkValue = rec
																									.get('id');
																							ids
																									.push(pkValue);
																						});

																		var resObj = self
																				.ajax({
																					url : "basicinfo/room!addRoomToGangwei.action",
																					params : {
																						gangweiids : ids
																								.join(","),
																						srcroombh : roombh
																					}
																				});
																		if (resObj.success) {
																			if (null != myMask)
																				myMask
																						.hide();
																			self
																					.msgbox(resObj.obj);
																			return false;
																		} else {
																			if (null != myMask)
																				myMask
																						.hide();
																			Ext.Msg
																					.alert(
																							"友情提示",
																							resObj.obj);
																			return false;
																		}
																	} catch (e) {
																		if (null != myMask)
																			myMask
																					.hide();
																		Ext.Msg
																				.alert(
																						"友情提示",
																						"与服务器通讯出现异常");
																	}

																	return false;
																}
															});

										}
									},
									"roomgangweigrid button[ref=back]" : {
										click : function(btn) {
											btn.ownerCt.ownerCt.ownerCt.close();
											return false;
										}
									},

									"roomgrid button[ref=toggleRoomOpenClose]" : {

										click : function(btn) {

											var grid = btn
													.up("panel[xtype=roomgrid]");
											var records = grid
													.getSelectionModel()
													.getSelection();
											Ext.Msg
													.confirm(
															"房间开关状态取反",
															"<center><h3>确定要将房间的开关状态取反?</h3></center>",
															function(result) {
																if (result == "yes") {
																	var ids = new Array();
																	Ext
																			.each(
																					records,
																					function(
																							rec) {
																						var pkValue = rec
																								.get('roombh');
																						ids
																								.push(pkValue);
																					});
																	// 发送ajax请求
																	try {
																		var resObj = self
																				.ajax({
																					url : "basicinfo/room!toggleRoomOpenClose.action",
																					params : {
																						roombhs : ids
																								.join(",")

																					}
																				});
																		if (resObj.success) {
																			// grid.getStore().load();
																			self
																					.msgbox(resObj.obj);
																			return false;
																		} else {
																			Ext.Msg
																					.alert(
																							'友情提示',
																							resObj.obj);
																			return false;
																		}
																	} catch (e) {
																		Ext.Msg
																				.alert(
																						'友情提示',
																						"与服务器通讯出现异常");
																	}
																}

															});

											return false;
										}
									},
									"roomgrid button[ref=delRoomToGw]" : {
										click : function(btn) {
											var grid = btn.ownerCt.ownerCt;
											var records = grid
													.getSelectionModel()
													.getSelection();
											if (records.lenght == 0) {
												Ext.Msg.alert("友情提示", "请选择房间");
												return false;
											} else if (records.lenght > 1) {
												Ext.Msg
														.alert("友情提示",
																"请选择一个房间");
												return false;
											}
											var roombh = records[0]
													.get('roombh');
											var roomname = records[0]
													.get('roomname');
											if (roombh == null || roombh == "") {
												Ext.Msg.alert("友情提示", "房间编号为空");
												return false;
											}
											if (roomname == null
													|| roomname == "") {
												Ext.Msg.alert("友情提示", "房间名称为空");
												return false;
											}
											var window = Ext
													.create(
															'Ext.window.Window',
															{
																title : '将房间从岗位上删除并自动同步',
																height : 500,
																width : 900,
																layout : 'fit',
																modal : true,
																items : {
																	xtype : 'roomgangweigrid'

																}
															});
											window.show();
											window
													.down(
															"textfield[name=srcroombh]")
													.setValue(roombh);
											window.down("button[ref=addToGangWei]").setText("将房间从所选岗位删除");
											window
													.down(
															"textfield[name=srcroomname]")
													.setValue(roomname);
											window.down("textfield[name=type]")
													.setValue("del");
											return false;
										}
									},
									"roomgrid button[ref=setRoomToGw]" : {
										click : function(btn) {
											var grid = btn.ownerCt.ownerCt;
											var records = grid
													.getSelectionModel()
													.getSelection();
											if (records.lenght == 0) {
												Ext.Msg.alert("友情提示", "请选择房间");
												return false;
											} else if (records.lenght > 1) {
												Ext.Msg
														.alert("友情提示",
																"请选择一个房间");
												return false;
											}
											var roombh = records[0]
													.get('roombh');
											var roomname = records[0]
													.get('roomname');
											if (roombh == null || roombh == "") {
												Ext.Msg.alert("友情提示", "房间编号为空");
												return false;
											}
											if (roomname == null
													|| roomname == "") {
												Ext.Msg.alert("友情提示", "房间名称为空");
												return false;
											}
											var window = Ext
													.create(
															'Ext.window.Window',
															{
																title : '将房间添加到岗位上并自动下载',
																height : 500,
																width : 900,
																layout : 'fit',
																modal : true,
																items : {
																	xtype : 'roomgangweigrid'

																}
															});
											window.show();
											window
													.down(
															"textfield[name=srcroombh]")
													.setValue(roombh);
											window
													.down(
															"textfield[name=srcroomname]")
													.setValue(roomname);
											window.down("button[ref=addToGangWei]").setText("将房间添加到所选岗位");
											window.down("textfield[name=type]")
													.setValue("add");
											return false;
										}
									},

									/**
									 * 添加房间
									 */
									"roomgrid button[ref=addRoom]" : {
										click : function(btn) {
											var resObj = self
													.ajax({
														url : "basicinfo/room!validateRight.action"
													});
											if (false == resObj.success) {

												Ext.Msg.alert('友情提示',
														resObj.obj);
												return false;

											}
											var window = Ext
													.create(
															'Ext.window.Window',
															{
																title : '添加房间',
																height : 380,
																width : 600,
																layout : 'fit',
																modal : true,
																items : {
																	xtype : 'addroomform',
																	id : 'addroomrform'
																}
															});
											window.show();
											return false;
										}
									},
									"panel[xtype=addroomform] button[ref=return]" : {
										click : function(btn) {
											btn.ownerCt.ownerCt.ownerCt.close();
											return false;
										}
									},
									"panel[xtype=roomform] button[ref=return]" : {
										click : function(btn) {
											btn.ownerCt.ownerCt.ownerCt.close();
											return false;
										}
									},
									"panel[xtype=addroomform] button[ref=addRoom]" : {
										click : function(btn) {

											var addroomform = btn
													.up("panel[xtype=addroomform]");
											var formObj = addroomform.getForm();
											var params = self
													.getFormValue(formObj);
											if (formObj.isValid()) {
												var resObj = self
														.ajax({
															url : "basicinfo/room!isExist.action",
															params : params
														});
												if (resObj.success) {
													var resObj2 = self
															.ajax({
																url : "basicinfo/room!addRoom.action",
																params : params
															});
													if (resObj2.success) {
														self.msgbox("添加房间成功");
														Ext.ComponentQuery
																.query("panel[xtype=roomgrid] component[xtype=pagingtoolbar]")[0]
																.moveFirst();
														btn.ownerCt.ownerCt.ownerCt
																.close();
														return false;
													} else {
														Ext.Msg.alert("友情提示",
																resObj2.obj);
														return false;
													}
												} else {
													Ext.Msg.alert('友情提示',
															resObj.obj);
													return false;
												}
											} else {
												Ext.Msg.alert('友情提示',
														"请检查要添加的房间信息是否正确");
												return false;
											}

											return false;

										}
									}

								});
					},
					views : [ "core.room.view.RoomLayout",
							"core.room.view.RoomGrid",
							"core.room.view.addRoomForm",
							"core.room.view.RoomForm",
							"core.room.view.RoomGwGrid" ],
					stores : [ "core.room.store.RoomStore",
							"core.room.store.HouseOptStore",
							"core.doorctrl.store.ControllerOptStore",
							"core.renshifuzu.store.GangWeiStore" ],
					models : [ "core.room.model.RoomModel",
							"core.room.model.HouseOptModel",
							"core.doorctrl.model.ControllerOptModel",
							'core.renshifuzu.model.GangWeiModel' ],
					checkEdit : function() {
						var grid = Ext.ComponentQuery
								.query("panel[xtype=roomgrid]")[0];
						var num = grid.getSelectionModel().getSelection().length;
						var deleteRoom = Ext.ComponentQuery
								.query("panel[xtype=roomgrid] button[ref=deleteRoom]")[0];
						var setRoomToGw = Ext.ComponentQuery
								.query("panel[xtype=roomgrid] button[ref=setRoomToGw]")[0];
						var delRoomToGw = Ext.ComponentQuery
								.query("panel[xtype=roomgrid] button[ref=delRoomToGw]")[0];

						if (deleteRoom != null) {
							deleteRoom.setDisabled(num == 0);
						}
						if (delRoomToGw != null) {
							delRoomToGw.setDisabled(num == 0);
						}
						if (setRoomToGw != null) {
							setRoomToGw.setDisabled(num != 1);
						}

					}
				});